package com.android.jay.lib_commin_ui.lettersidebar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.android.jay.lib_commin_ui.R;
import com.android.jay.lib_commin_ui.utils.StatusBarUtil;
import com.android.jay.lib_image_loader.image.Utils;

import androidx.annotation.Nullable;

/**
 * Created by wzp on 2020/2/22 0022.
 * <p>
 * 描述  ：自定义字母索引
 */
public class LetterSideBar extends View {

    private int mTextHighColor;
    private int mTextSize;

    private int mTextColor;
    private Paint mPaint;
    // 定义26个字母
    public static String[] mLetters = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};

    //当前触摸的位置字母
    private String mCurrentTonchLetter = "A";


    public LetterSideBar(Context context) {
        this(context, null);
    }

    public LetterSideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LetterSideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LetterSideBar);
        mTextColor = ta.getColor(R.styleable.LetterSideBar_textColor, Color.WHITE);
        mTextSize = ta.getInt(R.styleable.LetterSideBar_textSize, 12);
        mTextHighColor = ta.getInt(R.styleable.LetterSideBar_textHighLight, Color.RED);
        ta.recycle();
        mPaint = new Paint();
        mPaint.setColor(mTextColor);
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(StatusBarUtil.dip2px(context, mTextSize));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int textWidth = (int) mPaint.measureText("A");
//        int width = getPaddingRight() + getPaddingLeft() + textWidth;
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //计算每个文字的高度
        int itemHeight = (getHeight() - getPaddingBottom() - getPaddingTop()) / mLetters.length;
        for (int i = 0; i < mLetters.length; i++) {
            int textWidth = (int) mPaint.measureText(mLetters[i]);
            //每个字母的中心线
            int letterCenter = i * itemHeight + itemHeight / 2 + getPaddingTop();
            @SuppressLint("DrawAllocation") Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
            int dy = (fm.bottom - fm.top) / 2 - fm.bottom;
            float baseLine = letterCenter + dy;
            //居中X
            int x = getWidth() / 2 - textWidth / 2;
            Log.e("LetterSideBar","x=="+x +"baseLine= "+baseLine);
            //高亮显示
            if (mLetters[i].equals(mCurrentTonchLetter)) {
                mPaint.setColor(mTextHighColor);
                canvas.drawText(mLetters[i], x, baseLine, mPaint);
            } else {
                mPaint.setColor(mTextColor);
                canvas.drawText(mLetters[i], x, baseLine, mPaint);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                //算出当前触摸的是那个字母
                float currentY = event.getY();
                int itemHeight = (getHeight() - getPaddingBottom() - getPaddingTop()) / mLetters.length;
                int currentPosition = (int) (currentY / itemHeight);
                if (currentPosition < 0) {
                    currentPosition = 0;
                }
                if (currentPosition > mLetters.length - 1) {
                    currentPosition = mLetters.length - 1;
                }
                mCurrentTonchLetter = mLetters[currentPosition];
                if (mLetters != null) {
                    mListener.touch(mCurrentTonchLetter, true);
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if (mListener != null) {
                    mListener.touch(mCurrentTonchLetter, false);
                }
                break;
        }
        return true;
    }

    private LetterTouchListener mListener;

    public void setLetterTouchListener(LetterTouchListener listener) {
        this.mListener = listener;
    }


    public interface LetterTouchListener {
        void touch(String letter, boolean isTouch);
    }
}
